import storage from 'store'
import Vue from 'vue'
import {toLogin, getUserInfo} from "@/api/api";
import {ACCESS_ROLES, ACCESS_TOKEN, ACCESS_INFO} from '@/store/mutation-types'
import {welcome, getOneArr} from '@/utils/util'
import router from '@/router'
function setInfo(res, commit) {
    if (res.webMenus && (res.webMenus instanceof Array)) {
      const webMenu = getOneArr(res.webMenus);
      storage.set('webMenu', webMenu.map(e => e.modelName));
      commit('SET_MODELNAME', webMenu.map(e => e.modelName));
    }
  }

const avatar = require('@/assets/avatar.jpg')
const user = {
    state: {
        token: '',
        name: '',
        welcome: '',
        avatar: avatar,
        roles: [],
        info: storage.get(ACCESS_TOKEN) || {},
        dbType: {
            'add': '新增',
            'edit': '编辑',
            'detail': '详情',
        },
        modelName: [],
        roleName: ''
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_MODELNAME: (state, modelName) => {
            state.modelName = modelName;
          },
        SET_NAME: (state, {name, welcome}) => {
            state.name = name
            state.welcome = welcome
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_INFO: (state, info) => {
            state.info = info
        },
        SET_ROLENAME: (state, roleName) => {
            state.roleName = roleName
        },
    },

    actions: {
        // 登录
        Login({commit}, userInfo) {
            return new Promise((resolve, reject) => {
                toLogin(userInfo).then(response => {
                    const result = response
                    storage.set(ACCESS_TOKEN, result.token)
                    commit('SET_TOKEN', result.token)
                    commit('SET_ROLENAME', result.role)
                    storage.set(ACCESS_ROLES, result.adminMenus)
                    storage.set(ACCESS_INFO, result)

                    commit('SET_ROLES', result.adminMenus)
                    storage.set(ACCESS_ROLES, result.adminMenus)



                    commit('SET_INFO', result)
                    commit('SET_AVATAR', result.headPortrait)
                    resolve()
                }).catch(response => {
                    reject(response)
                    if(response.data) {
                        const result = response.data
                        storage.set(ACCESS_TOKEN, result.token)
                        commit('SET_TOKEN', result.token)
                        storage.set(ACCESS_INFO, result)
                        commit('SET_INFO', result)
                    }


                })
            })
        },

        // 获取用户信息
        GetInfo({commit}) {
            return new Promise((resolve, reject) => {
                getUserInfo().then(response => {
                    const result = response
                    storage.set(ACCESS_ROLES, result.adminMenus)
                    commit('SET_ROLES', result.adminMenus)
                    // storage.set(ACCESS_INFO, result)
                    // commit('SET_INFO', result)
                    // setInfo(result, commit);
                    // commit('SET_AVATAR', result.headPortrait)
                    resolve(response)
                }).catch(error => {
                    reject(error)
                })
            })
        },

        // 登出
        Logout({commit, state}) {
            return new Promise((resolve) => {
                commit('SET_TOKEN', '')
                commit('SET_ROLES', [])
                commit('SET_INFO', {})
                storage.remove(ACCESS_TOKEN)
                storage.remove(ACCESS_ROLES)
                storage.remove(ACCESS_INFO)
                router.replace({
                    path: sessionStorage.getItem('paths') || '/auth/login'
                })
                resolve()
            })
        }

    }
}

export default user
